//
//  HomeViewController.m
//  OCQ_API_Test
//
//  Created by 故乡的云  on 2017/5/3.
//  Copyright © 2017年 故乡的云 . All rights reserved.
//

#import "HomeViewController.h"
#import "ViewController.h"


@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource>
/// 分组标题
@property (nonatomic, strong) NSMutableArray *array;
/// 外部联系人
@property (nonatomic, strong) NSMutableArray *contacts;
/// 本地通讯录
@property (nonatomic, strong) NSArray <gxdy_Contact *>*locationAdds;
/// 分组header集合
@property (nonatomic, strong) NSMutableArray *headers;

//@property (nonatomic, weak) UITableView *tableView;
/// 每一组row的个数
@property (nonatomic, strong) NSMutableArray *numberOfRows;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//    [self.view addSubview:tableView];
//    _tableView = tableView;
//    tableView.contentInset = UIEdgeInsetsMake(20, 0, 44, 0);
//    tableView.delegate = self;
//    tableView.dataSource = self;
}

#pragma mark - table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.numberOfRows[section] integerValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    
    switch (indexPath.section) {
        case 0: {
            gxdy_Contact *contact = self.locationAdds[indexPath.row];
            cell.textLabel.text = contact.givenName;
            cell.detailTextLabel.text = contact.familyName;
//            cell.imageView.image = [UIImage imageWithData:contact.imageData];
        }
            break;
        case 2: cell.textLabel.text = self.contacts[indexPath.row];
            break;
            
        default:
            break;
    }
    cell.imageView.image = [UIImage imageNamed:@"baiduLogo"];
    
    return cell;
}

#pragma mark - table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    ViewController *VC = [ViewController new];
//    switch (indexPath.section) {
//        case 0: {
//            gxdy_Contact *contact = self.locationAdds[indexPath.row];
//            VC.title = contact.givenName;
//        }
//            break;
//        case 2: VC.title = self.contacts[indexPath.row];
//            break;
//            
//        default:
//            break;
//    }
//    [self.navigationController pushViewController:VC animated:YES];
//    [iToast showiToast:self.view text:@"这是测试消息"];
//    [iToast showiToast:self.view text:@"这是测试消息2"];
    [iToast showiToast:[UIApplication sharedApplication].keyWindow view:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iconfont-wechat-blue"]] Y:0];
    [iToast showiToast:nil view:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iconfont-wechat-blue"]] Y:90];
}

#pragma mark  折叠／展开
/// Header的生命周期短，reload时会释放，会重新创建一个新的header，
/// 所以需要保存header的数据状态，在创建时对其重新赋值
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *header;
    if (section < self.headers.count) {
        header = self.headers[section];
    }else {
        header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 25)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.view.bounds.size.width, 25)];
        label.text = self.array[section];
        label.font = [UIFont systemFontOfSize:13];
        UIButton *btn = [[UIButton alloc] initWithFrame:header.bounds];
        [btn addTarget:self action:@selector(showTable:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = section;
        [header addSubview:label];
        [header addSubview:btn];
        header.backgroundColor = [UIColor lightGrayColor];
        [self.headers addObject:header];
    }
    
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (void)showTable:(UIButton *)sw {
    sw.selected = !sw.isSelected;
    if (sw.isSelected) {
        switch (sw.tag) {
            case 0: self.numberOfRows[sw.tag] = @(self.locationAdds.count);
                break;
            case 2: self.numberOfRows[sw.tag] = @(self.contacts.count);
                break;
                
            default: self.numberOfRows[sw.tag] = @(0);
                break;
        }
    }else {
        self.numberOfRows[sw.tag] = @0;
    }
//    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:sw.tag] withRowAnimation:UITableViewRowAnimationNone]; // ?????? 报错
    [self.tableView reloadData];
}

#pragma mark  移动
/*
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (bool)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
        return YES;
    }else {
        return NO;
    }
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    NSLog(@"source:%zi destination:%zi",sourceIndexPath.row,destinationIndexPath.row);
}
 */

#pragma mark - proterties 

- (NSMutableArray *)contacts {
    if (_contacts == nil) {
        _contacts = [NSMutableArray array];
        self.contacts[0] = @"Nb";
        self.contacts[1] = @"Nb";
    }
    return _contacts;
}

- (NSArray *)locationAdds {
    if (_locationAdds == nil) {
        _locationAdds = [gxdy_AddressBookManager getAddressBookWithKeysToFetch:nil];
    }
    return _locationAdds;
}

- (NSMutableArray *)headers {
    if (_headers == nil) {
        _headers = [NSMutableArray array];
    }
    return _headers;
}

- (NSMutableArray *)array {
    if (_array == nil) {
        _array = [NSMutableArray array];
        _array[0] = @"本地通讯录(验证：不同版本数据是否统一！)";
        _array[1] = @"内部联系人";
        _array[2] = @"外部联系人";
        self.numberOfRows[0] = @0;
        self.numberOfRows[1] = @0;
        self.numberOfRows[2] = @0;
    }
    return _array;
}

- (NSMutableArray *)numberOfRows {
    if (_numberOfRows == nil) {
        _numberOfRows = [NSMutableArray array];
//        _numberOfRows[0] = @5;
//        _numberOfRows[1] = @0;
//        _numberOfRows[2] = @2;
    }
    return _numberOfRows;
}
@end
